package svc;

import constants.Constants;
import dao.ReproDao;
import domain.repro.Repro;
import domain.repro.ReproMisc;
import domain.repro.SeasonalityRepro;

public class ReproSvc {
	ReproDao dao = new ReproDao();
	
	public Repro getReproDetails(int fileNo){
		Repro result = new Repro();
		
		result.setMisc(dao.getReproMisc(fileNo));
		
		result.setAbortRisk(dao.getAbortRisk(fileNo));
		result.setConcRisk(dao.getConcRisk(fileNo));
		
		result.setSeasonalityConc(dao.getSeasonalityConc(fileNo));
		result.setPctConcMim(dao.getPctConcMim(fileNo));
		result.setConceptionRate(getConceptionRate(result.getSeasonalityConc(), result.getPctConcMim(), result.getMisc().getLbm()));
		
		result.setSeasonalityHeat(dao.getSeasonalityHeat(fileNo));
		result.setPctHeatMim(dao.getPctHeatMim(fileNo));
		result.setHeatDetectionRate(getHeatDetectionRate(result.getSeasonalityHeat(), result.getPctHeatMim(), result.getMisc().getLbm()));
		
		return result;
	}
	
	
	public double[][][] getConceptionRate(SeasonalityRepro seasonalityConc, double pctConcMim[], int lastBreedingMonth){
		double result[][][] = new double[Constants.LACT+1][Constants.MIM+1][Constants.SEA+1];
		
		double concAbs = seasonalityConc.getAbsAdjustment();
		double concSeasonality = seasonalityConc.getOverallSeasonality();
		double concSea[] = seasonalityConc.getMonthlySeasonality();
		double concSeaAvg = getMonthlySeasonalityAvg(seasonalityConc);
		
		for(int mim=3; mim <=lastBreedingMonth; mim++){
			for(int sea=1; sea <=Constants.SEA; sea++){
				result[0][mim-1][sea] = (pctConcMim[mim] * concSea[sea] * concSeasonality) + (pctConcMim[mim] * concSeaAvg * (1-concSeasonality)) + concAbs;
			}
		}
		
		return result;
	}
	
	public double[][] getHeatDetectionRate(SeasonalityRepro seasonalityHeat, double pctHeatMim[], int lastBreedingMonth){
		double result[][] = new double[Constants.MIM+1][Constants.SEA+1];
		
		double heatAbs = seasonalityHeat.getAbsAdjustment();
		double heatSeasonality = seasonalityHeat.getOverallSeasonality();
		double heatSea[] = seasonalityHeat.getMonthlySeasonality();
		double heatSeaAvg = getMonthlySeasonalityAvg(seasonalityHeat);
		
		for(int mim=3; mim <=lastBreedingMonth; mim++){
			for(int sea=1; sea <=Constants.SEA; sea++){
				result[mim-1][sea] = (pctHeatMim[mim] * heatSea[sea] * heatSeasonality) + (pctHeatMim[mim] * heatSeaAvg * (1-heatSeasonality)) + heatAbs;
			}
		}
		
		return result;
	}

	private double getMonthlySeasonalityAvg(SeasonalityRepro seasonalityRepro){
		double sum = 0;
		for(int i=1; i<=Constants.SEA; i++){
			sum += seasonalityRepro.getMonthlySeasonality()[i];
		}
		return sum/Constants.SEA;
	}
	
	public boolean updateReproDetails(int fileNo, Repro repro){
		boolean res1 = dao.updateAbortRisk(fileNo, repro.getAbortRisk());
		boolean res2 = dao.updateConcRisk(fileNo, repro.getConcRisk());
		boolean res3 = dao.updatePctConcMim(fileNo, repro.getPctConcMim());
		boolean res4 = dao.updatePctHeatMim(fileNo, repro.getPctHeatMim());
		boolean res5 = dao.updateReproMisc(fileNo, repro.getMisc());
		boolean res6 = dao.updateSeasonalityConc(fileNo, repro.getSeasonalityConc());
		boolean res7 = dao.updateSeasonalityHeat(fileNo, repro.getSeasonalityHeat());
	
		return (res1 && res2 && res3 && res4 && res5 && res6 && res7);
	}
	
	public boolean updateReproMisc(int fileNo, ReproMisc reproMisc){
		return dao.updateReproMisc(fileNo, reproMisc);
	}
	
}